package com.zzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Chat;
import com.zzj.entity.ChatFriends;
import com.zzj.model.dto.chat.QueryChatListDTO;
import com.zzj.model.dto.chat.SendDTO;
import com.zzj.service.IChatFriendsService;
import com.zzj.service.IChatService;
import com.zzj.service.RedisService;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 聊天表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-03-05
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private IChatService chatService;
    @Resource

    private IChatFriendsService chatFriendsService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RedisService redisService;

    @Transactional
    @PostMapping("queryChatList")
    public Result chat(@RequestHeader(value = "Authorization") String token,
                       @RequestBody @Validated QueryChatListDTO in) {
        String username = JWTUtil.getUsername(token);
        LambdaQueryWrapper<Chat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chat::getFromUsername, username)
                .eq(Chat::getToUsername, in.getToUsername())
                .or(i -> i.eq(Chat::getFromUsername, in.getToUsername())
                        .eq(Chat::getToUsername, username))
                .orderByDesc(Chat::getCreateTime);
        Page<Chat> chatPage = new Page<>(in.getPageNo(), in.getPageSize());
        IPage<Chat> chatIPage = chatService.getBaseMapper().selectPage(chatPage, queryWrapper);
        String item = in.getToUsername() + ",间隔," + username;
        redisService.setChatNoReadNum(item,0);
        return Result.success(chatIPage);
    }

    @Transactional
    @PostMapping("send")
    public Result send(@RequestHeader(value = "Authorization") String token,
                       @RequestBody @Validated SendDTO in) {
        String username = JWTUtil.getUsername(token);

        ChatFriends one = chatFriendsService.findChatFriend(username, in.getToUsername());
        if (one == null) {
            return Result.error("发送失败，未添加好友 ");
        }
        Chat chat = new Chat();
        chat.setContent(in.getContent());
        chat.setFromName(one.getFromName());
        chat.setFromUsername(one.getFromUsername());
        chat.setFromAvatar(one.getFromAvatar());
        chat.setToName(one.getToName());
        chat.setToUsername(one.getToUsername());
        chat.setToAvatar(one.getToAvatar());
        boolean save = chatService.save(chat);
        if (!save) {
            return Result.error("发送失败");
        }
        return Result.success("发送成功");
    }
}
